import 'dart:async';

import 'package:flutter/material.dart';

class FutureBuilderTest extends StatefulWidget {
  @override
  _FutureBuilderTestState createState() => _FutureBuilderTestState();
}

class _FutureBuilderTestState extends State<FutureBuilderTest> {
  final future0 = Future.delayed(Duration(seconds: 1), () => 11);

  final future1 = Future.delayed(Duration(seconds: 1), () => 12);

  final stream0 = Stream.periodic(Duration(seconds: 1), (_) => 13);

  final stream1 = Stream.periodic(Duration(seconds: 1), (_) => 14);

  final streamController = StreamController();

  int num = 0;

  @override
  void initState() {
    future0.then((value) => print('future value: $value'));
    // stream0.listen((event) {
    //   print('stream value: $event');
    // });
    super.initState();
  }

  @override
  void dispose() {
    streamController.close();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print('build----');
    return Scaffold(
      appBar: AppBar(title: Text('异步更新UI')),
      body: DefaultTextStyle(
        style: TextStyle(fontSize: 20, color: Colors.red),
        child: Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              Column(
                children: [
                  Text('FutureBuilder'),
                  SizedBox(height: 40),
                  FutureBuilder(
                      future: future1, //throw('error msg')
                      initialData: '加载中...',
                      builder: (BuildContext context,
                          AsyncSnapshot<dynamic> snapshot) {
                        print(snapshot.connectionState);
                        if (snapshot.hasError) {
                          return Text('error');
                        }
                        return Text('${snapshot.data}');
                      }),
                  SizedBox(height: 40),
                  FutureBuilder(
                      future: Future.delayed(Duration(seconds: 3),
                          () => throw ('error msg')), //throw('error msg')
                      initialData: '加载中...',
                      builder: (BuildContext context,
                          AsyncSnapshot<dynamic> snapshot) {
                        print(snapshot.connectionState);
                        if (snapshot.hasError) {
                          return Text('error');
                        }
                        return Text('${snapshot.data}');
                      }),
                ],
              ),
              Column(
                children: [
                  TextButton(
                      onPressed: () {
                        num++;
                        streamController.sink.add(num);
                      },
                      child: Text('FutureBuilder')),
                  SizedBox(height: 40),
                  TextButton(
                      onPressed: () {
                        streamController.sink.addError('error msg');
                      },
                      child: Text('error')),
                  SizedBox(height: 40),
                  TextButton(
                      onPressed: () {
                        streamController.close();
                      },
                      child: Text('关闭')),
                  SizedBox(height: 40),
                  StreamBuilder(
                      stream: streamController.stream,
                      builder: (BuildContext context,
                          AsyncSnapshot<dynamic> snapshot) {
                        switch (snapshot.connectionState) {
                          case ConnectionState.none:
                            return Text('none:没数据流');
                            break;
                          case ConnectionState.waiting:
                            return Text('waiting:等待数据流');
                            break;
                          case ConnectionState.active:
                            if (snapshot.hasError) {
                              return Text('active: error: ${snapshot.error}');
                            } else {
                              return Text('active: 正常: ${snapshot.data}');
                            }

                            break;
                          case ConnectionState.done:
                            return Text('done:数据流关闭');
                            break;
                        }
                        return Container();
                      }),
                  SizedBox(height: 40),
                  StreamBuilder(
                      stream: stream1,
                      initialData: '加载中...',
                      builder: (BuildContext context,
                          AsyncSnapshot<dynamic> snapshot) {
                        switch (snapshot.connectionState) {
                          case ConnectionState.none:
                            return Text('none:没数据流');
                            break;
                          case ConnectionState.waiting:
                            return Text('waiting:等待数据流');
                            break;
                          case ConnectionState.active:
                            if (snapshot.hasError) {
                              return Text('active: error: ${snapshot.error}');
                            } else {
                              return Text('active: 正常: ${snapshot.data}');
                            }

                            break;
                          case ConnectionState.done:
                            return Text('done:数据流关闭');
                            break;
                        }
                        return Container(child: Text(''));
                      })
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}
